package com.xuef.async;

import com.alibaba.fastjson.JSON;
import com.xuef.util.JedisAdapter;
import com.xuef.util.RedisKeyGenerateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by moveb on 2018/5/22.
 */
@Service
public class EventConsumer implements InitializingBean, ApplicationContextAware {
    private static final Logger logger = LoggerFactory.getLogger(EventConsumer.class);
    private ApplicationContext applicationContext;
    private Map<EventType, List<EventHandler>> eventsMap = new HashMap<>();

    @Autowired
    private JedisAdapter jedisAdapter;

    @Override
    public void afterPropertiesSet() throws Exception {
        // 创建一个 handler 路由表，以进行后面的事件转发
        // 找到所有实现了 EventHandler接口 的Handler
        Map<String, EventHandler> handlers = applicationContext.getBeansOfType(EventHandler.class);
        // 将 eventType 与 handlers 组织起来
        if(handlers != null){
            for(Map.Entry<String, EventHandler> entry : handlers.entrySet()){
                EventHandler eventHandler = entry.getValue();// EventHandler 实例
                // 其感兴趣的事件类型
                List<EventType> supportEventTypes = eventHandler.getSupportEventTypes();
                for(EventType et: supportEventTypes){
                    if(!eventsMap.containsKey(et)){
                        eventsMap.put(et, new ArrayList<>());
                    }else{
                        eventsMap.get(et).add(eventHandler);
                    }
                }
            }
        }
        /*
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while(true){
                    String key = RedisKeyGenerateUtil.getEventQueueKey();
                    // brpop???
                    List<String> eventList = jedisAdapter.brpop(0, key);
                    for(String eventString:eventList){
                        // ????
                        if(eventString.equals(key)){
                            continue;
                        }
                        EventModel eventModel = JSON.parseObject(eventString, EventModel.class);
                        if(!eventsMap.containsKey(eventModel.getEventType())){
                            logger.info("不能处理的事件");
                            continue;
                        }
                        List<EventHandler> eventHandlers = eventsMap.get(eventModel.getEventType());
                        for(EventHandler eventHandler: eventHandlers){
                            eventHandler.doHandle(eventModel);
                        }
                    }
                }
            }
        });
        thread.start();;
        */
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
